package com.example.yiqiuser.controller;


import com.example.yiqiuser.entity.Visitor;
import com.example.yiqiuser.service.VisitorService;
import com.example.yiqiuser.util.Page;
import com.example.yiqiuser.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;

/**
 * @author: Jyc
 * @classname: VisitorController
 * @date: 2023/3/30
 * @time: 20:34
 * @description:游客控制层
 */
@RestController
@RequestMapping("visit")
public class VisitorController extends BaseController {
    @Resource
    private VisitorService visitorService;

    /**
     * 分页带参查询
     * @param page
     * @return
     */
    @PostMapping("queryPage")
    public Result queryByPage(@RequestBody Page<Visitor> page) {
        return success(visitorService.queryByParam(page));
    }

    /**
     * 游客添加
     * @param visitor
     * @return
     */
    @PostMapping("add")
    public Result adds(@RequestBody Visitor visitor) {
        return success(visitorService.adds(visitor));
    }

    /**
     * 删除游客
     * @param id
     * @return
     */
    @DeleteMapping("deleteById")
    public Result deleteById(int id) {
        return success(visitorService.deleteById(id));
    }

    /**
     * 游客更新
     * @param visitor
     * @return
     */
    @PostMapping("update")
    public Result update(@RequestBody Visitor visitor) {
        return success(visitorService.update(visitor));
    }

    @PostMapping("accountAndPassword")
    public Result accountAndPassword(String account,String password){
        Visitor visitor = new Visitor();
        visitor.setPassword(password);
        visitor.setAccount(account);
        return success(visitorService.queryByAccountAndPassword(visitor));
    }

    @GetMapping("phoneNUmAndPassword")
    public Result phoneNUmAndPassword(String phoneNum, String password, HttpSession session){
        Visitor visitor = new Visitor();
        visitor.setPhoneNum(phoneNum);
        visitor.setPassword(password);
        /*System.out.println("手机号:"+visitor.getPhoneNum()+", 密码:"+visitor.getPassword());*/
        Visitor visitor1 = visitorService.queryByPhoneNumAndPassword(visitor);
        session.setAttribute("visitorName",visitor1.getNickName());
        return success(visitor1);
    }

}
